package com.ekuaitu.kuaitu.map;

import android.graphics.Color;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.PolylineOptions;

/**
 * Created by Michael Pi on 2016/11/22.
 */
public class PolylineTraceUtils {
    private static AMap aMap;
    private LatLng lastPosition;
    private int totalDistance = 0;
    private static PolylineTraceUtils instance;

    public static PolylineTraceUtils getInstance(AMap aMap) {
        if (instance == null) {
            instance = new PolylineTraceUtils(aMap);
        }
        return instance;
    }

    private PolylineTraceUtils(AMap aMap) {
        PolylineTraceUtils.aMap = aMap;
    }


    public int addTracePolyline(LatLng curPosition) {
        if (lastPosition == null) {
            lastPosition = curPosition;
        }
        float distance = AMapUtils.calculateLineDistance(lastPosition, curPosition);
        totalDistance += distance;
        if (distance >= 10) {
            aMap.addPolyline((new PolylineOptions())
                    .add(lastPosition, curPosition)
                    .width(10)
                    .color(Color.argb(255, 83, 126, 220)));
            lastPosition = curPosition;
        }
        aMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(curPosition, aMap.getCameraPosition().zoom, 0, 0)), 1500, null);
        return totalDistance;
    }
}
